TypeScript v4.4
2021/8/12
Control Flow Analysis of Aliased Conditions
いったん定数に入れた後にも分岐できるようになった
code:ts
type Shape =
| { kind: 'circle'; radius: number }
| { kind: 'square'; sideLength: number };
function area(shape: Shape): number {
const { kind } = shape;
if (kind === 'circle') {
return Math.PI * shape.radius ** 2;
}
return shape.sideLength ** 2;
}
従来は、if (shape.kind === 'circle')のように書かないとダメだったmrsekut.icon
条件式を変数に入れて、使うタイミングをずらしてもguardできる
code:ts
function f(x: string | number | boolean) {
const isString = typeof x === 'string';
const isNumber = typeof x === 'number';
const isStringOrNumber = isString || isNumber;
if (isStringOrNumber) {
x; // 'string | number'.
} else {
x; // 'x' is 'boolean'.
}
}
従来は、if (typeof x === 'string' || typeof x === 'number')と書かないとダメだったmrsekut.icon
コード例が良いねmrsekut.icon
Symbol and Template String Pattern Index Signatures
keyにsymbolとTemplate Literal Typesを使えるようになった
code:ts
interface Colors {
}
code:ts
interface OptionsWithDataProps extends Options {
[optName: data-${string}]: unknown;
}
catch (err)のerrが、もともとanyだったがunknownになる
recordのoptional?: ..なpropertyに、undefinedを代入することを禁止する
classにstaticblockを導入
code:ts
class Foo {
static Foo.count = 0;
static {
if (someCondition()) {
Foo.count++;
}
}
}
というかこれ、ECMAScriptに入っているんだmrsekut.icon
Spelling Suggestions for JavaScript
@ts-checkを使ったときのJSのスペルチェック
Auto-Imports Show True Paths in Completion Lists
補完するときのpathが見やすくなった
色々速くなった